package com.ymt.bpm.web.rest.platform.fauth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PGroup;
import com.ymt.bpm.service.platform.fauth.FaPgService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.EncodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.QueryParam;
import java.util.Map;

/**
 * Created by Johnny on 2017/5/4.
 */
@RestController
@RequestMapping("/api/v1/fa/pg")
public class FaPgResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaPgService pgs;

    @GetMapping("/query")
    public JSONObject query(HttpServletRequest req,
                            @RequestParam(name="search", required = false)String search,
                            @RequestParam(name="sort", required = false)String sort,
                            @RequestParam(name="order", required = false)String order,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (search!=null && search.length()>0) {
            search = EncodeUtil.decode(search);
        }
        if (log.isDebugEnabled()) {
            log.debug("query tenantId=" + tenantId);
            log.debug("query search=" + search);
            log.debug("query sort=" + sort);
            log.debug("query order=" + order);
            log.debug("query offset=" + offset);
            log.debug("query limit=" + limit);
            log.debug("query timezoneOffset=" + timezoneOffset);
        }
        return pgs.query(tenantId, search, sort, order, offset, limit, timezoneOffset);
    }

    /**
     * get a group
     * @param req
     * @param groupId
     * @return
     */
    @GetMapping(value="/{groupId}")
    public JSONObject get(HttpServletRequest req,
                          @PathVariable("groupId")String groupId,
                          @QueryParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("get tenantId=" + tenantId);
            log.debug("get groupId=" + groupId);
        }
        return pgs.get(tenantId, groupId, Const.LANG_ZH, timezoneOffset);
    }

    /**
     * check an attribute of a group
     * @param req
     * @param groupId
     * @return
     */
    @PostMapping(value="/checkGroupName",
            produces = "plain/text")
    public String checkPosName(HttpServletRequest req,
                               @RequestParam(value="groupId", required=false)String groupId,
                               @RequestParam("groupName")String groupName) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkGroupName tenantId=" + tenantId);
            log.debug("checkGroupName groupName=" + groupName);
            log.debug("checkGroupName groupId=" + groupId);
        }
        return pgs.checkAttr(tenantId, "GroupName", groupName, groupId);
    }

    /**
     * save a group
     * @param req
     * @param group
     * @return
     */
    @PostMapping("/save")
    public JSONObject saveDetailed(HttpServletRequest req,
                                   PGroup group) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("save tenantId=" + tenantId);
            log.debug("save group.getGroupId()=" + group.getGroupId());
            log.debug("save group.getGroupName()=" + group.getGroupName());
        }
        return pgs.save(tenantId, loginName, displayName, group);
    }

    /**
     * delete a group
     * @param req
     * @param groupId
     * @return
     */
    @DeleteMapping("/delete/{groupId}")
    public JSONObject delete(HttpServletRequest req,
                             @PathVariable("groupId")String groupId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete tenantId=" + tenantId);
            log.debug("delete groupId=" + groupId);
        }
        return pgs.delete(tenantId, loginName, displayName, groupId);
    }

    /**
     * delete group-pos by groupId
     * @param req
     * @param groupId
     * @return
     */
    @DeleteMapping("/delSubPos/{groupId}")
    public JSONObject delSubPos(HttpServletRequest req,
                                  @PathVariable("groupId")String groupId,
                                  @RequestParam("posId")String posId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delSubPos tenantId=" + tenantId);
            log.debug("delSubPos groupId=" + groupId);
            log.debug("delSubPos posId=" + posId);
        }
        return pgs.delSubPos(tenantId, loginName, displayName, groupId, posId);
    }

    /**
     * clear group-pos-user by groupId
     * @param req
     * @param groupId
     * @param posId
     * @return
     */
    @DeleteMapping("/delSubPosUser/{groupId}")
    public JSONObject delSubPosUser(HttpServletRequest req,
                                      @PathVariable("groupId")String groupId,
                                      @RequestParam("posId")String posId,
                                      @RequestParam("userId")String userId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delSubPosUser tenantId=" + tenantId);
            log.debug("delSubPosUser groupId=" + groupId);
            log.debug("delSubPosUser posId=" + posId);
        }
        return pgs.delSubPosUser(tenantId, loginName, displayName, groupId, posId, userId);
    }

    /**
     * clear group-pos by groupId
     * @param req
     * @param groupId
     * @return
     */
    @DeleteMapping("/clearSubPos/{groupId}")
    public JSONObject clearSubPos(HttpServletRequest req,
                             @PathVariable("groupId")String groupId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("clearSubPos tenantId=" + tenantId);
            log.debug("clearSubPos groupId=" + groupId);
        }
        return pgs.clearSubPos(tenantId, loginName, displayName, groupId);
    }

    /**
     * clear group-pos-user by groupId
     * @param req
     * @param groupId
     * @param posId
     * @return
     */
    @DeleteMapping("/clearSubPosUser/{groupId}")
    public JSONObject clearSubPosUser(HttpServletRequest req,
                             @PathVariable("groupId")String groupId,
                             @RequestParam("posId")String posId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("clearSubPosUser tenantId=" + tenantId);
            log.debug("clearSubPosUser groupId=" + groupId);
            log.debug("clearSubPosUser posId=" + posId);
        }
        return pgs.clearSubPosUser(tenantId, loginName, displayName, groupId, posId);
    }

    /**
     * add group-pos by posIds
     * @param req
     * @param groupId
     * @return
     */
    @PostMapping("/addSubPos")
    public JSONObject addSubPos(HttpServletRequest req,
                                  @RequestParam("groupId")String groupId,
                                  @RequestParam("posIds")String posIds) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("addSubPos tenantId=" + tenantId);
            log.debug("addSubPos groupId=" + groupId);
            log.debug("addSubPos posIds=" + posIds);
        }
        return pgs.addSubPos(tenantId, loginName, displayName, groupId, posIds);
    }

    /**
     * add group-pos-user by userIds
     * @param req
     * @param groupId
     * @return
     */
    @PostMapping("/addSubPosUser")
    public JSONObject addSubPosUser(HttpServletRequest req,
                                      @RequestParam("groupId")String groupId,
                                      @RequestParam("posId")String posId,
                                      @RequestParam("userIds")String userIds) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("addSubPosUser tenantId=" + tenantId);
            log.debug("addSubPosUser groupId=" + groupId);
            log.debug("addSubPosUser posId=" + posId);
            log.debug("addSubPosUser userIds=" + userIds);
        }
        return pgs.addSubPosUser(tenantId, loginName, displayName, groupId, posId, userIds);
    }

    @GetMapping("/querySubPos")
    public JSONObject querySubPos(HttpServletRequest req,
                            @RequestParam(name="groupId")String groupId,
                            @RequestParam(name="sort", required = false)String sort,
                            @RequestParam(name="order", required = false)String order,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("querySubPos tenantId=" + tenantId);
            log.debug("querySubPos groupId=" + groupId);
            log.debug("querySubPos sort=" + sort);
            log.debug("querySubPos order=" + order);
            log.debug("querySubPos offset=" + offset);
            log.debug("querySubPos limit=" + limit);
            log.debug("querySubPos timezoneOffset=" + timezoneOffset);
        }
        return pgs.querySubPos(tenantId, groupId, sort, order, offset, limit, timezoneOffset);
    }

    @GetMapping("/querySubPosUser")
    public JSONObject querySubPosUserquerySubPosUser(HttpServletRequest req,
                            @RequestParam(name="groupId")String groupId,
                            @RequestParam(name="posId")String posId,
                            @RequestParam(name="sort", required = false)String sort,
                            @RequestParam(name="order", required = false)String order,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("querySubPosUser tenantId=" + tenantId);
            log.debug("querySubPosUser groupId=" + groupId);
            log.debug("querySubPosUser posId=" + posId);
            log.debug("querySubPosUser sort=" + sort);
            log.debug("querySubPosUser order=" + order);
            log.debug("querySubPosUser offset=" + offset);
            log.debug("querySubPosUser limit=" + limit);
            log.debug("querySubPosUser timezoneOffset=" + timezoneOffset);
        }
        return pgs.querySubPosUser(tenantId, groupId, posId, sort, order, offset, limit, timezoneOffset);
    }

    @GetMapping("/allByTenantWithPos")
    public JSONArray allByTenantWithPos(HttpServletRequest req) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("allByTenantWithPos tenantId=" + tenantId);
        }
        return pgs.allByTenantWithPos(tenantId);
    }

}
